package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiji.www.frameworks.utils.DensityUtils;

import cn.com.dhc.danlu.R;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com) Date: 2016-05-08 Time: 12:11
 * Description: .....
 */
public class CustomItemLayout extends RelativeLayout {

    private int mDefaultItemHeight = DensityUtils.dip2px(getContext(), 45);

    private int mLeftImgResourceId;
    private String mDescription;
    private String mTips;
    private int mTipColor;

    private int mLeftIconWidth;
    private int mLeftIconHeight;

    private boolean isShowArrow = true;

    public CustomItemLayout(Context context) {
        this(context, null);
    }

    public CustomItemLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.dl_myself_item_layout_style);
        mDefaultItemHeight = (int) typedArray.getDimension(R.styleable.dl_myself_item_layout_style_item_height,
                mDefaultItemHeight);
        mLeftImgResourceId = typedArray.getResourceId(R.styleable.dl_myself_item_layout_style_item_left_icon, 0);
        mDescription = typedArray.getString(R.styleable.dl_myself_item_layout_style_item_description);
        mTips = typedArray.getString(R.styleable.dl_myself_item_layout_style_item_tips);
        mTipColor = typedArray.getColor(R.styleable.dl_myself_item_layout_style_item_tips_color,
                Color.parseColor("#888888"));
        isShowArrow = typedArray.getBoolean(R.styleable.dl_myself_item_layout_style_item_show_arrow, true);
        mLeftIconHeight = (int) typedArray.getDimension(R.styleable.dl_myself_item_layout_style_item_left_icon_height, DensityUtils.dip2px(getContext(), 23));
        mLeftIconWidth = (int) typedArray.getDimension(R.styleable.dl_myself_item_layout_style_item_left_icon_width, DensityUtils.dip2px(getContext(), 23));

        typedArray.recycle();
        bindView();
    }

    private void bindView() {
        setBackgroundResource(R.drawable.selector_list_item);
        setMinimumHeight(mDefaultItemHeight);

        addLeftImageView();
        addArrowView();
        addDescription();
        addTipsView();
    }

    private void addDescription() {
        if (TextUtils.isEmpty(mDescription)) {
            return;
        }
        TextView textView = new TextView(getContext());
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_VERTICAL);

        if (mLeftImgResourceId == 0) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            lp.setMargins(DensityUtils.dip2px(getContext(), 28), 0, 0, 0);
        } else {
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.mil_leftimg);
            lp.setMargins(DensityUtils.dip2px(getContext(), 10), 0, 0, 0);
        }
        textView.setTextSize(14);
        textView.setTextColor(getResources().getColor(R.color.color_444444));
        textView.setText(mDescription);
        textView.setLayoutParams(lp);
        textView.setId(R.id.mil_des);
        addView(textView);
    }

    private void addLeftImageView() {
        if (mLeftImgResourceId == 0) {
            return;
        }
        ImageView imageView = new ImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        LayoutParams lp = new LayoutParams(mLeftIconWidth,
                mLeftIconHeight);
        lp.addRule(RelativeLayout.CENTER_VERTICAL | RelativeLayout.ALIGN_PARENT_LEFT);
        lp.setMargins(DensityUtils.dip2px(getContext(), 15), 0, 0, 0);
        imageView.setImageResource(mLeftImgResourceId);
        imageView.setLayoutParams(lp);
        imageView.setId(R.id.mil_leftimg);
        addView(imageView);
    }

    private void addTipsView() {
        TextView textView = new TextView(getContext());
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

        if (isShowArrow) {
            lp.setMargins(DensityUtils.dip2px(getContext(), 10), 0, DensityUtils.dip2px(getContext(), 5), 0);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.addRule(RelativeLayout.LEFT_OF, R.id.mil_arrow);
        } else {
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.setMargins(DensityUtils.dip2px(getContext(), 10), 0, DensityUtils.dip2px(getContext(), 15), 0);
        }
        lp.addRule(RelativeLayout.RIGHT_OF, R.id.mil_des);
        textView.setGravity(Gravity.RIGHT);
        textView.setSingleLine();
        textView.setEllipsize(TruncateAt.END);
        textView.setTextColor(mTipColor);
        // maxlength = 20
        // textView.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20) });
        textView.setTextSize(12);
        textView.setId(R.id.mil_tips);
        textView.setText(!TextUtils.isEmpty(mTips) ? mTips : "");
        textView.setLayoutParams(lp);
        addView(textView);

    }

    /**
     * 外排一个方法供宿主界面调用刷新
     */
    public void refreshTipText(String string) {
        View view = findViewById(R.id.mil_tips);
        if (null != view && view instanceof TextView) {
            TextView tipTextView = (TextView) view;
            tipTextView.setText(string);
        }
    }

    public void setIcArrowIsVisible(boolean isVisible) {
        View view = findViewById(R.id.mil_arrow);
        if (null != view) {
            view.setVisibility(isVisible ? VISIBLE : INVISIBLE);
        }
    }

    public void refreshTipText(String string, int maxLength) {
        if (TextUtils.isEmpty(string)) {
            return;
        }
        View view = findViewById(R.id.mil_tips);
        if (null != view && view instanceof TextView) {
            TextView tipTextView = (TextView) view;
            if (string.length() > 10) {
                string += "...";
            }
            tipTextView.setText(string);
        }
    }


    private void addArrowView() {
        if (isShowArrow) {
            ImageView imageView = new ImageView(getContext());
            LayoutParams lp = new LayoutParams(DensityUtils.dip2px(getContext(), 13),
                    DensityUtils.dip2px(getContext(), 13));
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            lp.setMargins(0, 0, DensityUtils.dip2px(getContext(), 15), 0);
            imageView.setImageResource(R.drawable.ic_arrow);
            imageView.setLayoutParams(lp);
            imageView.setId(R.id.mil_arrow);
            addView(imageView);
        }
    }
}

